package com.example.neteasecloudmusic_kotlin.ui.homepage

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.example.neteasecloudmusic_kotlin.R
import com.example.neteasecloudmusic_kotlin.logic.model.HomePageEntity.Creative
import kotlinx.android.synthetic.main.playlist_item.view.*

class PlayListItemAdapter(private val playListItem: List<Creative>) : RecyclerView.Adapter<PlayListItemAdapter.ViewHolder>() {

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val songListImage : ImageView = view.songListImage
        val songListTitle : TextView = view.songListTitle
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.playlist_item, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val playItem = playListItem[position]
        val context = holder.itemView.context
        holder.songListTitle.text = playItem.title
        Glide.with(holder.itemView)
            .load(playItem.imageUrl)
            .apply(RequestOptions.bitmapTransform(RoundedCorners(30)))
            .into(holder.songListImage)
        if (position == playListItem.size - 1) {
            holder.itemView.setPadding(dip2px(context, 10.toFloat()), 0, dip2px(context, 10.toFloat()), 0)
        }
    }

    private fun dip2px(context: Context?, dpValue: Float): Int {
        val scale = context!!.resources.displayMetrics.density
        return (dpValue * scale + 0.5).toInt()
    }

    override fun getItemCount() = playListItem.size
}